

local Camera = class("Camera")

function Camera:ctor(map)
	self.map_ = map
	local scale = map:getScale()
	self.mapSize = self.map_:getMapSize()
	self.tileSize = cc.size(self.map_:getTileSize().width * scale, self.map_:getTileSize().height * scale)  
	self.mapSizePx = cc.size(self.mapSize.width * self.tileSize.width, self.mapSize.height * self.tileSize.height)


end

function Camera:move(disX, disY, time) 
	local disX = disX or 0
	local disY = disY or 0
	local posX, posY = self.map_:getPosition()

	if posX + disX > display.left then  -- 等价 pos.x < display.cx  表示人物距离左边界 位置小于半屏
		disX = display.left - posX  --只让地图移到边界
	elseif posX + self.mapSizePx.width + disX < display.right then --self.mapSizePx.width -pos.x < display.cx  表示人物距离右边界 位置小于半屏
		disX = display.right - posX - self.mapSizePx.width--只让地图移到边界
	end

	if posY + disY > display.bottom then
		disY = display.bottom - posY
	elseif posY + self.mapSizePx.height + disY < display.top then
		disY = display.top - posY - self.mapSizePx.height
	end

	if time then
		self.map_:stopAllActions()
		self.map_:moveTo({x = posX + disX, y = posY + disY, time = time})
	else
		self.map_:setPosition(posX + disX, posY + disY)
	end

end

function Camera:look(entity, time)
	local p = entity:getMapPos()

	local pos = cc.p((p.x - 0.5) * self.tileSize.width, (self.mapSize.height - p.y + 0.5) * self.tileSize.height)

	local mapPosX, mapPosY = self.map_:getPosition()
	self:move(display.cx - mapPosX - pos.x, display.cy - mapPosY - pos.y, time)
end

return Camera
